package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @PackageName:
 * @ClassName: MenuServiceImpl
 * @Author: sushengchao
 * @Date: 2019/12/11 12:23
 * @Description:
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuMapper;

    @Override
    public List<Menu> findByRoleId(Integer id) {
        return null;
    }

    @Override
    public List<Menu> findMenu(Set<Role> set) {
        if(set == null){
            return null;
        }
        List<Menu> list = new ArrayList<>();
        //遍历角色集合
        for (Role role : set) {
            //根据角色id查询得到一级菜单集合
            List<Menu> menuList = menuMapper.findMenu1(role.getId());
            if(menuList == null){
                continue;
            }
            //遍历得到每个一级菜单对象
            for (Menu menu : menuList) {
                Map<String,Integer> map = new HashMap<>();
                map.put("roleId",role.getId());
                map.put("menuId",menu.getId());
                List<Menu> list1 = menuMapper.findMenu2(map);
                if(list1 == null){
                    continue;
                }else {
                    menu.setChildren(list1);
                }
                list.add(menu);
            }
        }
        return list;
    }
}
